package zju.dgm.dto;

import lombok.Data;

import java.io.Serializable;
import java.util.Arrays;


@Data
public class DataFrame implements Serializable {
    public static final int ERR = -1;
    public static final int HELLO = 0;
    public static final int CLIENT_REQUEST = 10001;    // client发给server的请求
    public static final int SERVER_REQUEST = 10002;    // server之间的请求
    public static final int CLIENT_RESPOND = 10003;    // server给client的回复
    public static final int SERVER_RESPOND = 10004;    // server之间的回复
    public static final int MASTER_WRITE_REQ = 10005;    // master向Server发送的写请求
    public static final int SERVER_TRANSFER_WRITE = 10006;    // Server转发写请求至Master
    public static final int UNDO_REQUEST = 10007;    // Server转发写请求至Master
    public static final int LOG_INFO = 10008;    // 获取日志文件
    public static final int REDO_RESPOND = 10009;    // 根据日志文件重建表的结果


    // regionService
    public static final int MASTER_MAP_REQ      = 20001;     // 向master请求map信息
    public static final int MASTER_MAP_RES      = 20002;     // master返回map信息
    public static final int MASTER_VERSION_REQ  = 20003;     // 向master请求版本信息
    public static final int MASTER_VERSION_RES  = 20004;     // master返回版本信息
    public static final int MASTER_FREE_REQ     = 20005;     // 向master请求空闲server信息
    public static final int MASTER_FREE_RES     = 20006;     // master返回空闲server信息
    public static final int MASTER_ADD_REQ      = 20007;     // 向master请求写map
    public static final int MASTER_DELETE_REQ   = 20008;     // 向master请求删map
    public static final int LOG_REQ             = 20009;     // 向region请求log

    private String sender;
    private int type;
    private byte[] data;

    /**
     * 用于JSON序列化
     */
    public DataFrame() {
    }

    public DataFrame(String sender, int type, byte[] data) {
        this.sender = sender;
        this.type = type;
        this.data = data;
    }

    public DataFrame(DataFrame df) {
        if(this==df){
            return;
        }
        sender = df.sender;
        type = df.type;
        data = Arrays.copyOf(df.data,df.data.length);
    }

    public DataFrame(String sender) {
        this(sender, HELLO, null);
    }

    public DataFrame(byte[] data) {
        this("anonymous", HELLO, data);
    }
}
